package org.epes.laboratory.vcn.service;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.el.Expression;
import org.epes.laboratory.vcn.application.OptimizationApplication;
import org.epes.laboratory.vcn.utils.POICMISHelper;

public class StoreOutputsTask implements JavaDelegate {
	
	private Expression analysisDocumentId;
	private Expression analysisFolderId;	

	private static final String EXCEL_MIMETYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";	
		
	public void execute(DelegateExecution execution) throws Exception {
		//TODO Implement the filling out of the analysis excel
		OptimizationApplication oa = (OptimizationApplication) execution.getVariable("optimizationApplication");
				
		// Calculate the real CO2 emissions
		oa.setRealCalculatedEmissions(Double.parseDouble(oa.getRealDistance())*oa.getCo2ManagedKnowledge());
		//oa.setRealCalculatedEmissions(Float.parseFloat(realDistance)*oa.getCo2ManagedKnowledge());
		
		// Read excel analysis from the VCN
		// Get the excel analysis EPES_Lab_ANALYSIS.xlsx from the VCN using CMIS		
		POICMISHelper helper = new POICMISHelper();		
		String cmisWorkbookId = analysisDocumentId.getValue(execution).toString();
		helper.openWorkbook(cmisWorkbookId);
		
		int newRowNumber = helper.getNewRowNumber();	
				
		// Add a new line to the excel file
		helper.setCellValue(oa.getExcelSolutionName(), 
				newRowNumber, 11, true);
		helper.setCellValue(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()), 
				newRowNumber, 1, true);
		helper.setCellValue(Integer.parseInt(oa.getTheoreticalOptimalSequence()), 
				newRowNumber, 2, true);
		helper.setCellValue(Double.parseDouble(oa.getTheoreticalOptimalDistance()), 
				newRowNumber, 3, true);
		helper.setCellValue(oa.getCo2ManagedKnowledge(), 
				newRowNumber, 4, true);
		helper.setCellValue(Double.parseDouble(oa.getTheoreticalCalculatedEmissions()), 
				newRowNumber, 5, true);
		helper.setCellValue(Integer.parseInt(oa.getRealSequence()), 
				newRowNumber, 7, true);
		helper.setCellValue(Double.parseDouble(oa.getRealDistance()),
				newRowNumber, 8, true);
		helper.setCellValue(oa.getRealCalculatedEmissions(),
				newRowNumber, 9, true);
		
		// Evaluate all the cells with formulas
		helper.evaluateAll();	
		
		//helper.saveDocumentToFileSystem("EPES_Lab_ANALYSIS.xlsx");
		
		// Save the excel analysis to the VCN
		ByteArrayOutputStream excelAnalysis = helper.getExcelAnalysis();
		helper.saveDocumentVersionToFolder(excelAnalysis, analysisFolderId.getValue(execution).toString(), 
				"EPES_Lab_ANALYSIS", ".xslx", EXCEL_MIMETYPE, cmisWorkbookId);	
				
		// Set excel analysis as a field of the process variable optimizationApplication
		oa.setExcelAnalysis(excelAnalysis.toByteArray());
		
		// Update the optimizationApplication process variable
		execution.setVariable("optimizationApplication", oa);
								
		System.out.println("Service is executed!");
	}
}
